﻿using PRO.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using PRO.Common;

namespace PRO.Public
{
    public class EmailHelper
    {
        /// <summary>
        /// 使用MailMessage发送邮件
        /// </summary>
        /// <param name="to">发送给。。多个邮件用逗号隔开</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="fileupLoadPath">附件绝对地址</param>
        /// <param name="nick">对方看到的昵称</param>
        /// <param name="msg">如果发送失败返回失败信息</param>
        /// <returns></returns>
        public static bool SendEmail(string to, string subject, string body, string fileupLoadPath, string nick, out string msg)
        {
            msg = "";
            //根据,分隔成多个邮件
            string[] ts = to.Split(',');
            //是否成功
            bool isSuccess = true;

            try
            {
                //创建发送电子邮件类
                MailMessage message = new MailMessage();
                //设置发送人邮件地址
                message.From = new MailAddress(BaseConfigs.EmailAccount, nick, System.Text.Encoding.UTF8);
                //循环发送
                for (int i = 0; i < ts.Length;i++ )
                {
                    if (ts[i].IsEmail())
                        //将收件人邮件地址添加收件人电子邮件集合中
                        message.To.Add(ts[i].Trim());
                }
                //设置电子邮件主题
                message.Subject = subject;
                //设置邮件正文是否可以是HTML标签
                message.IsBodyHtml = true;
                //设置电子邮件的优先级
                message.Priority = MailPriority.High;
                //设置邮件正文
                message.Body = body;
                //设置主题和正文编码为UTF8
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                //获取附加到电子邮件的附件集合
                if (fileupLoadPath.Length != 0)
                {
                    message.Attachments.Add(new Attachment(fileupLoadPath));
                }
                //抄送收件人,将邮件发给收件人以外的人
                //message.CC.Add("Tianc@163.com");
                //密件抄送收件人,不想别人看到我同时发信给这个邮箱,那就用上吧
                //message.Bcc.Add("Tianc@126.com");
                //设置邮件的回复地址
                //message.ReplyTo = new MailAddress("Tianc@163.com");

                //使用简单邮件传输协议来发送邮件
                SmtpClient sc = new SmtpClient();
                //设置stmp邮件服务器地址
                sc.Host = BaseConfigs.EmailHost;
                //设置stmp服务器端口,这里使用163的端口
                sc.Port = BaseConfigs.EmailPort;
                /*随身份验证信息一起发送,false表示不发送身份验证信息
                有些 SMTP 服务器要求在代表客户端发送电子邮件前验证客户端的身份*/
                sc.UseDefaultCredentials = true;
                //如果服务器不支持ssl则报服务器不支持安全连接错误
                //Secure Socket Layer（SSL:安全套接字层）是一种能将在用户端与伺服器端间传送的邮件加密的通讯协定

                sc.EnableSsl = BaseConfigs.EnableSSL;
                //验证发件人身份凭据
                sc.Credentials = new System.Net.NetworkCredential(BaseConfigs.EmailAccount, BaseConfigs.EmailPwd);
                //指定通过网络发送电子邮件
                sc.DeliveryMethod = SmtpDeliveryMethod.Network;
                //将指定邮件发送到STMP服务器
                sc.Send(message);
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                isSuccess = false;
            }

            return isSuccess;
        }
    }
}
